package com.googlecode.thriftbenchmark.twitter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * Factory to make random {@link Tweet}s.
 * 
 * @author Li Weibo (weibo.leo@gmail.com) //I believe spring-brother
 */
public class TweetFactory {

    private Random random = new Random();
    
    public Tweet randomTweet() {
        
        Tweet tweet = new Tweet();
        tweet.setUserId(randomUserId());
        tweet.setUserName("User-" + tweet.getUserName());
        tweet.setText(randomText());
        tweet.setTweetType(randomTweetType());
        tweet.setLocation(randomLocation());
        return tweet;
    }
    
    public List<Tweet> randomTweets(int n) {
        List<Tweet> tweets = new ArrayList<Tweet>(n);
        for (int i = 0; i < n; i++) {
            tweets.add(randomTweet());
        }
        return tweets;
    }
    
    private int randomUserId() {
        return 100000 + random.nextInt(100000);
    }
    
    private Location randomLocation() {
        Location loc = new Location();
        loc.setLatitude(180 * random.nextDouble());
        loc.setLongitude(360 * random.nextDouble());
        return loc;
    }
    
    private static final String TEXT_SET = "Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml.";
    private String randomText() {
        int randomLength = random.nextInt(TEXT_SET.length());
        return TEXT_SET.substring(0, randomLength);
    }

    private static TweetType[] ALL_TYPE = new TweetType[]{TweetType.DM, TweetType.REPLY, TweetType.RETWEET, TweetType.TWEET};
    private TweetType randomTweetType() {
        return ALL_TYPE[random.nextInt(ALL_TYPE.length)];
    }
    
}
